
package com.fivebrms.view.main
{
	import org.osflash.thunderbolt.Logger;
	
	import mx.core.Application;

	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.controls.Label;
	import mx.controls.Alert;

	import mx.events.FlexEvent;
	import mx.events.StateChangeEvent;
	
	import mx.rpc.remoting.RemoteObject;
	import mx.rpc.AsyncToken;
	
	import com.adobe.cairngorm.control.CairngormEventDispatcher
		
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.fivebrms.events.W9FormSendEvent;
	import com.fivebrms.events.LoginEvent;
	
	import com.fivebrms.model.MainModelLocator;

	public class Main extends Application
	{
		private var model : MainModelLocator;
		
		public var btn_login : Button;
		public var txt_login : TextInput;
		public var txt_password : TextInput;
		
		public function Main()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
        private function creationCompleteHandler(event:FlexEvent) : void
        {
            Logger.info("loaded main application...created model objects singletons..");
            
            this.model = MainModelLocator.getInstance();
            //
            // copy a reference to this container object to the model
            // so that when an login event occurs the login command may
            // asynchrnously switch the currentState on this container
            //
            this.model.main = this;
            //
			// successfully logged into application
			//
			btn_login.addEventListener(MouseEvent.CLICK, loginButtonHandler);
			
            //
            // listen to events on the url
            //
            this.urlListener();
		}
		
		//
		// login into the fbrms forms application
		//
        private function loginButtonHandler(event : MouseEvent) : void
        {
   			Logger.info("start - loginButtonHandler button click event..");

			//
			// set the form fields into a value object for transportation
			// to the server
			//
   			this.model.loginFormVO.login = txt_login.text;
   			this.model.loginFormVO.password = txt_password.text;
   			
   			Logger.info("setting the login vo credentials...");
   			Logger.info("is valid login --1--" + this.model.loginFormVO.isLoginSuccessful );
   			
   			//
            // login into the forms application
            //            
            var loginEvent : LoginEvent = new LoginEvent( );
            loginEvent.dispatch();
                       
   			Logger.info("end - loginButtonHandler button click event..");
		}
		
		
		private function urlListener() : void
		{
			Logger.info("start - main url manager..1..");
			
			this.initSWFAddress();
			this.addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE , setSWFAddress);
			
			Logger.info("end - main url manager..2..");
		}
		                
		private function handleSWFAddress() : void 
		{
			Logger.info("sweet..atleast reading the url..3.1.");
			
			var title:String = "Five Brothers Mortgage Company Services and Securing Inc.";
			SWFAddress.setTitle(title);
			
			
			Logger.info("sweet..atleast reading the url..3.2.");
		}
		
		private function setSWFAddress(stateChangeEvent : StateChangeEvent) : void 
		{
			var value:String = '/';
			
			if (currentState != '' && currentState != null) 
			{
				value += currentState.toLowerCase() + '/';
			}
			
			if (value != SWFAddress.getValue())
			{
				SWFAddress.setValue(value);
			}
		
			Logger.info("main..atleast reading the url..2..");
		}  
		    
		private function initSWFAddress() : void 
		{
		    SWFAddress.onChange = handleSWFAddress;
		    Logger.info("sweet..atleast reading the url..1..");
		} 
	}
}
